package com.sxbang.bms.controller;

import com.sxbang.bms.base.result.Results;
import com.sxbang.bms.dao.RoleDao;
import com.sxbang.bms.dto.RoleDto;
import com.sxbang.bms.model.SysRole;
import com.sxbang.bms.page.PageTableHandler;
import com.sxbang.bms.page.PageTableRequest;
import com.sxbang.bms.page.PageTableResponse;
import com.sxbang.bms.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.NumberUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("role")
@Slf4j
public class RoleController {

	@Autowired
	private RoleService roleService;
    @Autowired
    private RoleDao roleDao;

	@GetMapping("/{id}")
	public SysRole role(@PathVariable Integer id) {
		return roleService.getRoleById(id);
	}

	@RequestMapping("/all")
	@ResponseBody
	public Results<SysRole> getAll() {
		Results<SysRole> results = new Results<SysRole>();
		results.setCode("0");
		results.setCount(50);
		results.setData(roleService.getAllRoles());
		results.setMsg("test");
		log.info("getUsers()--------------");
        log.debug("getUsers()--------------");
		return results;
	}

    @GetMapping
    @RequestMapping("/listPage")
    @ResponseBody
    public Results list(PageTableRequest request) {
//        PageTableResponse pageTableResponse = new PageTableHandler(new PageTableHandler.CountHandler() {
//
//            @Override
//            public int count(PageTableRequest request) {
////                return userService.count(request.getParams());
//                return 1;
//            }
//        }, new PageTableHandler.ListHandler() {
//
//            @Override
//            public List<SysRole> list(PageTableRequest request) {
////                System.out.println(request);
////                System.out.println(request.toString()+"， "+request.getOffset()+"， "+request.getLimit());
//
//                List<SysRole> list = null;
//                return list;
////                return null;
//            }
//        }).handle(request);
        request.countOffset();
        return roleService.getByPage(request.getOffset(), request.getLimit());
//        return new Results().success(roleDao.countAll().intValue(), roleDao.getByPage(request.getOffset(), request.getLimit()));
    }
    @RequestMapping("/findUserByFuzzyUserName")
    @ResponseBody
    public Results getUserByFuzzyUserName(HttpServletRequest request,PageTableRequest requests) {
        String roleName = request.getParameter("roleName");
        requests.countOffset();
        return roleService.getRoleByFuzzyRoleNamePage(roleName,requests.getOffset(),requests.getLimit());
    }
	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public String editUser(Model model, SysRole user) {
		model.addAttribute("sysRole",roleService.getRoleById(user.getId()));
		return "role/role-edit";
	}

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public Results<SysRole> updateRole(@RequestBody RoleDto roleDto) {
        int a = roleService.updateRole(roleDto);
        return Results.success();
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String addUser(Model model) {
        model.addAttribute("sysRole",new SysRole());
        return "role/role-add";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Results<SysRole> saveRole(@RequestBody RoleDto roleDto) {
        roleService.saveRole(roleDto);
        return Results.success();
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    @ResponseBody
    public Results<SysRole> deleteRole(RoleDto roleDto) {
        roleService.deleteRole(roleDto.getId());
        return Results.success();
    }

    String pattern = "yyyy-MM-dd";
    //只需要加上下面这段即可，注意不能忘记注解
    @InitBinder
    public void initBinder(WebDataBinder binder, WebRequest request) {
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat(pattern), true));// CustomDateEditor为自定义日期编辑器
    }
}
